package projetstackoverflow

class Author {
	
	
	String pseudo
	String mail
	String password
	String profile
	Date registrationDate
	Boolean admin
	
	static hasMany = [messages:Message, votes:Vote]
	
    static constraints = {
		pseudo(blank:false)
		mail(blank:false)
    }
	
    static mapping = {
		profile(type:'text')
	}
	
	
	def String toString() {
		return "Pseudo : "+pseudo+", Mail : "+mail;
	}
	
	def Integer computeReputation()
	{
		//+5 for a Question
		//+10 for an Answer
		//+40 if the answer is Selected
		//+1 for a Vote
		//+(votesSum)
		def reputation = 0
		for(Message message : messages)
		{
			if(message instanceof Question)
			{
				reputation += 5
			}
			else if(message instanceof Answer)
			{
				reputation += 10
				if(message.selected)
					reputation += 40
			}
			reputation += message.countVotes()
		}
		
		for(Vote vote : votes)
		{
			if(vote.value != Vote.NO_VOTE)
				reputation ++;
		}
		
		return reputation
	}
}
